#!/bin/sh
# PCP QA Test No. 1577
# Exercise the BPF PMDA CO-RE fsslower module - install, remove and values.
#
# Copyright (c) 2022 Sohaib Mohamed.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.bpf
. ./common.bcc	# for _unmount_filesystem

_pmdabpf_check
which mkfs.ext4 >/dev/null 2>&1 || _notrun "mkfs.ext4 not installed"
_pmdabpf_require_kernel_version 5 0
_pmdabpf_require_libbpf_version 0 7

write_size=`expr 20 \* 1024 \* 1024` # number of bytes to write into testfile
out_file=$tmp.mount.ext4/tmpfile

status=1       # failure is the default!
signal=$PCP_BINADM_DIR/pmsignal

cat <<EOF >$tmp.conf
# Installed by PCP QA test $seq on `date`
[fsslower.so]
enabled=true
filesystem=ext4
EOF
_pmdabpf_tryload $tmp.conf

_cleanup()
{
    cd $here
    _unmount_filesystem ext4
    _pmdabpf_cleanup
    $sudo rm -rf $tmp $tmp.*
}

_prepare_pmda bpf
trap "_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

# real QA test starts here
_pmdabpf_install $tmp.conf
_pmdabpf_wait_for_metric

# mount ext4 before installing the PMDA, as mounting
# ext4 may trigger loading the ext4 kernel module
_mount_filesystem ext4

# Generate system activity for the CO-RE fsslower module
dd if=/dev/zero of=${out_file} bs=${write_size} count=1 conv=fsync 2>/dev/null
dd if=${out_file} of=/dev/null bs=${write_size} count=1 2>/dev/null

echo "=== report metric values for bytes ==="
pminfo -dfmtT bpf.fsslower.bytes 2>&1 | tee -a $seq_full \
| _value_filter_nonzero

echo "=== report metric values for comm ==="
pminfo -dfmtT bpf.fsslower.comm 2>&1 | tee -a $seq_full \
| _value_filter_any

echo "=== report metric values for filename ==="
pminfo -dfmtT bpf.fsslower.filename 2>&1 | tee -a $seq_full \
| _value_filter_any

echo "=== report metric values for file_op ==="
pminfo -dfmtT bpf.fsslower.file_op 2>&1 | tee -a $seq_full \
| _value_filter_any

echo "=== report metric values for lat ==="
pminfo -dfmtT bpf.fsslower.lat 2>&1 | tee -a $seq_full \
| _value_filter_nonzero

echo "=== report metric values for pid ==="
pminfo -dfmtT bpf.fsslower.pid 2>&1 | tee -a $seq_full \
| _value_filter_nonzero

echo "=== report metric values for offset ==="
pminfo -dfmtT bpf.fsslower.offset 2>&1 | tee -a $seq_full \
| _value_filter_any


_pmdabpf_remove

status=0
exit
